<template>
  <div>
    <!--    搜索表单-->
    <div style="margin-bottom: 20px">
      <!--      v-mode v-model绑定变量,使其可以进行搜索;@click使其可以搜索-->
      <el-input style="width:240px" placeholder="请输入名称" v-model="params.name"></el-input>
      <el-input style="width:240px;margin-left: 5px"placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"></i> 搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh-right"></i> 重置</el-button>
    </div>
    <!--    表格区域-->
    <el-table :data="tableData" stripe>
      <el-table-column prop="id" label="编号" width="80px"></el-table-column>
      <el-table-column prop="username" label="会员号"></el-table-column>
      <el-table-column prop="name" label="名称" width="150px"></el-table-column>
      <el-table-column prop="age" label="年龄" width="80px"></el-table-column>
      <el-table-column prop="address" label="地址" width="80px"></el-table-column>
      <el-table-column prop="phone" label="联系方式"></el-table-column>
      <el-table-column prop="sex" label="性别" width="80px"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
<!--          scope.row就是当前行数据;scope.row.id获取当前行id @click方法实现点击后跳转,并显示数据信息-->
          <el-button type="primary" @click="$router.push('/editUser?id=' + scope.row.id)">
            编辑
          </el-button>

          <el-popconfirm
              style="margin-left: 4px"
              title="您确定删除这行数据吗?"
              @confirm="del(scope.row.id)">
            <el-button type="danger" slot="reference">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页功能,来自element ui-->
    <!--    current-page和page-size绑定分页组件,提供当前页码以及当前每一个个数跟数据绑定-->
    <div style="margin-top: 20px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'User',
  data() {
    return {
      tableData: [],
      total: 0,
      //  绑定值
      params: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        phone: ''
      }
    }
    // components: {
    //   HelloWorld
    // }
  },
  created(){
    //  页面加载后调用method方法
    this.load()
  },
  //前端常采用anxious来处理数据
  methods:{
    load(){
      //res代表的就是mysql表中的那个数组,用fetch方法不太方便
      // fetch('http://localhost:9090/user/list').then(res => res.json()).then(res => {
      //   console.log(res)
      //   this.tableData=res
      // })
      request.get('/user/page',{
        params:this.params
      }).then(res => {
        if(res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset(){
      this.params={
        pageNum: 1,
        pageSize: 10,
        name: '',
        phone: ''
      }
      this.load()
    },
    handleCurrentChange(pageNum){
      //  点击分页按钮实现分页
      this.params.pageNum=pageNum
      this.load()
    },
    del(id){
      request.delete("/user/delete/" +id).then(res => {
        if(res.code === '200'){
          this.$notify.success("删除成功")
          this.load()
        }else{
          this.$notify.error(res.msg)
        }
      })
    }

  }
}
</script>

<style scoped>

</style>